home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / vsc92nov.zip / except.c < prev    next >
C/C++ Source or Header  |  1992-11-02  |  3KB  |  162 lines

  1. /*
  2.  * except.c -- Implementation of Scheme exception handling
  3.  *
  4.  * (C) m.b (Matthias Blume), Wed May 13 19:03:03 MET DST 1992, HUB/Ger
  5.  *         Humboldt-University of Berlin, Germany
  6.  */
  7.  
  8. # ident "@(#)except.c    (C) M.Blume, Humboldt-Uni Berlin, 1.2"
  9.  
  10. # include <stdio.h>
  11. # include <stdarg.h>
  12. # include <stdlib.h>
  13. # include <string.h>
  14.  
  15. # include "Cont.h"
  16. # include "String.h"
  17. # include "except.h"
  18. # include "m-except.h"
  19. # include "io.h"
  20. # include "storext.h"
  21. # include "type.h"
  22. # include "mode.h"
  23.  
  24. # define MESSAGE_BUF_SIZE 256
  25.  
  26. static char *message_buf = NULL;
  27. static unsigned message_buf_len = 0;
  28. static unsigned message_len = 0;
  29.  
  30. static void message_putc (int c, void *ignore)
  31. {
  32.   char *tmp;
  33.  
  34.   if (message_len >= message_buf_len) {
  35.     if (message_buf_len == 0)
  36.       tmp = malloc (MESSAGE_BUF_SIZE);
  37.     else
  38.       tmp = realloc (message_buf, message_buf_len + MESSAGE_BUF_SIZE);
  39.     if (tmp == NULL)
  40.       fatal ("Out of message buffer space");
  41.     message_buf = tmp;
  42.     message_buf_len += MESSAGE_BUF_SIZE;
  43.   }
  44.   message_buf [message_len++] = c;
  45. }
  46.  
  47. static void message (
  48.   const char *prefix, const char *template, va_list ap, putc_proc pp, void *cd)
  49. {
  50.   void *x;
  51.   char *s;
  52.   int i;
  53.   unsigned int u;
  54.   double d;
  55.   char buf [128];
  56.  
  57.   putc_string (prefix, pp, cd);
  58.   while (template [0]) {
  59.     if (template [0] == '%') {
  60.       switch (template[1]) {
  61.       case '%':
  62.     (* pp) (template[1], cd);
  63.     break;
  64.       case 'w':
  65.     x = va_arg (ap, void *);
  66.     write_object (x, pp, cd);
  67.     break;
  68.       case 'd':
  69.     x = va_arg (ap, void *);
  70.     display_object (x, pp, cd);
  71.     break;
  72.       case 's':
  73.     s = va_arg (ap, char *);
  74.     putc_string (s, pp, cd);
  75.     break;
  76.       case 'i':
  77.     i = va_arg (ap, int);
  78.     sprintf (buf, "%d", i);
  79.     putc_string (buf, pp, cd);
  80.     break;
  81.       case 'u':
  82.     u = va_arg (ap, unsigned);
  83.     sprintf (buf, "%u", u);
  84.     putc_string (buf, pp, cd);
  85.     break;
  86.       case 'f':
  87.     d = va_arg (ap, double);
  88.     sprintf (buf, "%f", d);
  89.     putc_string (buf, pp, cd);
  90.     break;
  91.       default:
  92.     --template;
  93.     break;
  94.       }
  95.       ++template;
  96.     } else
  97.       (* pp) (template [0], cd);
  98.     ++template;
  99.   }
  100.   va_end (ap);
  101. }
  102.  
  103. static ScmString *message_string (
  104.   const char *prefix, const char *template, va_list ap)
  105. {
  106.   ScmString *string;
  107.  
  108.   message_len = 0;
  109.   message (prefix, template, ap, message_putc, NULL);
  110.   string = getmem (ScmType (String), sizeof (ScmString) + message_len - 1);
  111.   string->length = message_len;
  112.   memcpy (string->array, message_buf, message_len);
  113.   return string;
  114. }
  115.  
  116. void warning (const char *text, ...)
  117. {
  118.   va_list ap;
  119.   va_start (ap, text);
  120.   message ("warning: ", text, ap, file_putc, stderr);
  121.   putc ('\n', stderr);
  122. }
  123.  
  124. volatile void error (const char *text, ...)
  125. {
  126.   void *eh, *tmp;
  127.   va_list ap;
  128.  
  129.   va_start (ap, text);
  130.   tmp = message_string ("error: ", text, ap);
  131.   eh = ScmMode (SCM_ERROR_HANDLER_MODE);
  132.   ScmRaiseError (eh, tmp);
  133. }
  134.  
  135. volatile void fatal (const char *text)
  136. {
  137. # ifndef EXIT_VOLATILE
  138.   extern volatile void exit (int);
  139. # endif
  140.  
  141.   fprintf (stderr, "fatal: %s\n", text);
  142.   exit (EXIT_FAILURE);
  143. }
  144.  
  145. volatile void restriction (const char *text, ...)
  146. {
  147.   void *eh, *tmp;
  148.   va_list ap;
  149.  
  150.   va_start (ap, text);
  151.   tmp = message_string (
  152.         "violation of an implementation restriction: ", text, ap);
  153.   eh = ScmMode (SCM_ERROR_HANDLER_MODE);
  154.   ScmRaiseError (eh, tmp);
  155. }
  156.  
  157. volatile void reset (const char *text)
  158. {
  159.   fprintf (stderr, "System reset: %s\n", text);
  160.   ScmRaiseReset ();
  161. }
  162.